﻿using FakeItEasy;
using NJabber.Client.StanzaFactories;
using NJabber.Stream.Elements;
using NUnit.Framework;
using System;

namespace NJabber.Client
{
    [TestFixture]
    public class PresenceSubscriptionResponderTests
    {
        private IXmppConnection connection;
        private ISubscriptionStanzaFactory factory;
        private JID jid = "some-jid@dom.com";
        private PresenceSubscriptionResponder responder;

        [SetUp]
        public void SetUp()
        {
            connection = A.Fake<IXmppConnection>();
            A.CallTo(() => connection.Jid).Returns(jid);
            factory = new SubscriptionStanzaFactory();
            responder = new PresenceSubscriptionResponder(connection, factory);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Constructor_NullParameter_ThrowsException()
        {
            responder = new PresenceSubscriptionResponder(null, null);
        }

        [Test]
        public void SendApproveResponse_SomeJid_SendsSubscriptionApproveStanza() 
        {             
            Stanza expectedStanza = factory.CreateSubscriptionApproveResponse(jid);
            responder.SendApproveResponse(jid);
            A.CallTo(() => connection.Stream.Write(expectedStanza)).MustHaveHappened();
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SendApproveResponse_NullArgument_ThrowsException()
        {
            responder.SendApproveResponse(null);
        }

        [Test]
        public void SendDenyResponse_SomeJid_SendsSubscriptionDenyStanza()
        {
            Stanza expectedStanza = factory.CreateSubscriptionDenyResponse(jid);
            responder.SendDenyResponse(jid);
            A.CallTo(() => connection.Stream.Write(expectedStanza)).MustHaveHappened();
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SendDenyResponse_NullArgument_ThrowsException()
        {
            responder.SendDenyResponse(null);
        }
    }
}
